package com.qf.ABA;

/**
 * @author WzYu
 * @date 2022-7-13 下午 2:43
 */
public class ABATest2 {
    public static int count = 0;

    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            new Thread(()->{
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //该方法线程不安全，因为count增加不是原子性，可能会导致count最终答案小于200
                for (int j=0;j<100;j++){
                    synchronized (ABATest2.class){
                        count++;
                    }
                }
            }).start();
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("count="+count);
    }
}